package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;

import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;



@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //页面提交的密码加密处理
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //根据页面提交的用户名查询
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        //校验emp非空
        if (emp == null) {
            return R.fail("用户名错误,请重新输入");
        }
        log.info("emp对象的结果是:====>>>>+{}", emp);
        //用户名正确,校验密码,查询到的/前端传递过来之后加密的password
        if (!emp.getPassword().equals(password)) {
            return R.fail("密码错误,请重新输入");
        }
        //用户名密码都正确,校验status
        if (emp.getStatus() == 0) {
            return R.fail("该账号已禁用");
        }
        //用户存在,将员工id存到Session中
        request.getSession().setAttribute("employee", emp.getId());
        //响应结果
        return R.success(emp);

    }

    //登出
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        //删除session中存储的用户id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> insert(@RequestBody Employee employee) {
        log.info("员工信息+{}", employee.toString());
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //补充其他null字段
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long emplId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(emplId);
//        employee.setUpdateUser(emplId);
        //非空校验在前端已经做了
        //保存员工信息
        employeeService.save(employee);
        //响应数据
        return R.success("保存成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        //log.info("page={},pageSize={},name={}",page,pageSize,name);
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        //添加条件1.模糊查询2.排序,条件name非空判断
        qw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        qw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, qw);
        return R.success(pageInfo);

    }

    //修改(编辑,启用,禁用)
    @PutMapping()
    public R<String> update( @RequestBody Employee employee) {
        log.info("请求参数+{}", employee.toString());

//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    //编辑表格数据回显
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }



}
